<?php
	include('simple_html_dom.php');

	//ham get source cua imgs trong site voi duong link cho truoc
	function getImgInfo($urlSite){
		$scheme = parse_url($urlSite,0);
		$host = parse_url($urlSite,1);
	
		function dlPage($href) {

		    $curl = curl_init();
		    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		    curl_setopt($curl, CURLOPT_HEADER, false);
		    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
		    curl_setopt($curl, CURLOPT_URL, $href);
		    curl_setopt($curl, CURLOPT_REFERER, $href);
		    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
		    curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.125 Safari/533.4");
		    $str = curl_exec($curl);
		    curl_close($curl);

			// Create a DOM object
		    $dom = new simple_html_dom();
		    // Load HTML from a string
		    $dom->load($str);

		    return $dom;
		    }
		//tao Dom de xu ly tung element
		$html = dlPage($urlSite);

		//tim the title cua site
		$title = $html->find('title',0);
		$infoImgs[] = $title->innertext;
		

		//tim tat ca the img cua file html
		$imgEls = $html->find('img');

		//$max_count la bien dem tong phan tu cua $els
		$max_count = 0;
		foreach ($imgEls as $el) {
			$max_count++;
		}
		
		// truong hop neu max_count < 30 thi chay het
		if ($max_count<30){
			foreach ($imgEls as $el) {
				//lay tat ca element img
				$imgSrc = $el->src;

				//kiem tra khong la file anh gif
				if(strtolower(substr($imgSrc, -4)) == ('.jpg' || '.png') || strtolower(substr($imgSrc, -5)) == '.jpeg') {

					//kiem tra thu src cua anh co chua host và http khong va tao duong link hoan chinh
					if(is_null(parse_url($imgSrc,0))){
						//kiem tra scheme co http khong
						if(substr($imgSrc,0,1)=='/'){
							$imgSrc = $scheme.'://'.$host.$imgSrc;
						} elseif (substr($imgSrc,0,1)=='.') {
							if (substr($imgSrc,1,1)=='.') {
								//neu src co dang ../img/
								$imgSrc = $scheme.'://'.$host.substr($imgSrc,2);
							} elseif (substr($imgSrc,1,1)=='/') {
								//neu src co dang ./img
								$imgSrc = $scheme.'://'.$host.substr($imgSrc,1);
							}
						} else{
							$imgSrc = $scheme.'://'.$host.'/'.$imgSrc;
						}
					} //end if cua truong hop khong co http
					
					//kiem tra width, height cua image
					list($width,$height)  = @getimagesize($imgSrc);
					if($width > 250 && $height > 250){
						//xuat ra the html img cua hinh get duoc
						$infoImgs[] = $imgSrc;
					}
				}
				
			}
		} else{

			$i=0 ; //bien dem de break khi vuot muc 30 vong lap
			$valImg = 0; // bien dem nhung img co kich thuoc hop chuan >x200 width heigh

			//duyet qua tung phan tu trong the dom img
			foreach ($imgEls as $el) {
				//lay tat ca element img
				$imgSrc = $el->src;

				//kiem tra khong la file anh gif
				if(strtolower(substr($imgSrc, -4)) == ('.jpg' || '.png') || strtolower(substr($imgSrc, -5)) == '.jpeg') {

					//kiem tra thu src cua anh co chua host và http khong va tao duong link hoan chinh
					if(is_null(parse_url($imgSrc,0))){
						//kiem tra scheme co http khong
						if(substr($imgSrc,0,1)=='/'){
							$imgSrc = $scheme.'://'.$host.$imgSrc;
						} elseif (substr($imgSrc,0,1)=='.') {
							//truong hop có cham '.'
							if (substr($imgSrc,1,1)=='.') {
								//neu src co dang ../img/
								$imgSrc = $scheme.'://'.$host.substr($imgSrc,2);
							} elseif (substr($imgSrc,1,1)=='/') {
								//neu src co dang ./img
								$imgSrc = $scheme.'://'.$host.substr($imgSrc,1);
							}
						} else{
							$imgSrc = $scheme.'://'.$host.'/'.$imgSrc;
						}//end if truong hop image/upload....
					} //end if cua truong hop khong co http
					
					//kiem tra width, height cua image
					list($width,$height)  = @getimagesize($imgSrc);
					if($width > 250 && $height > 250){
						//xuat ra the html img cua hinh get duoc
						$infoImgs[] = $imgSrc;
						$valImg++;
					}
				}
				$i++;
				if($valImg>1){
					if($i>30){
						break;//vuot qua 30 vong thi thoat ra
					}
				}
			}
			echo $i;
		}//end else cua truong hop max_count > 30
		
		return $infoImgs;
	}
	
?>